<template>
  <div class="suvery-list-wrap">
    <myHeader title="调查问卷" showBack @handleBack="handleDone" :customFunc="true" />
    <div class="form-box">
      <div class="form-input-container">
        <u-form :model="formModel" labelPosition="left" labelWidth="180rpx"
          :labelStyle="{ 'fontWeight': 'bold', 'font-size': '30rpx', }" ref="uForm">
          <u-form-item :label="item.title" prop="title" borderBottom v-for="(item, index) in suveryList" :key="index">
            <u-input v-if="item.type == 0" input-align="right" v-model="item.pitch" border="none"></u-input>
            <picker v-if="item.type == 1" @change="bindPickerChange" :value="item.pitch" :range="item.value">
              <view class="uni-input">{{ item.value[item.pitch].label }}</view>
            </picker>
            <u-radio-group v-if="item.type == 2" v-model="item.pitch" size="32rpx" placement="column">
              <u-radio label-size="32rpx" icon-size="32rpx" :customStyle="{ marginBottom: '8px' }"
                v-for="(citem, cindex) in item.value" :key="cindex" :label="citem.label" :name="citem.value">
              </u-radio>
            </u-radio-group>
            <u-checkbox-group v-if="item.type == 3" v-model="item.pitch" size="32rpx" placement="column">
              <u-checkbox shape="square" label-size="32rpx" icon-size="32rpx" :customStyle="{ margin: '20rpx 0' }"
                v-for="(citem, cindex) in item.value" :key="cindex" :label="citem.label" :name="citem.value">
              </u-checkbox>
            </u-checkbox-group>
          </u-form-item>
        </u-form>
        <div style="margin: 20rpx 0;">
          <u-button type="primary" @click="handleDone">确定</u-button>
        </div>
      </div>

    </div>

  </div>
</template>
<script>
import { getSurveyList } from "../../api";
export default {
  data() {
    return {
      suveryList: []
    }
  },
  onLoad() {
    this.getSurveyList()
  },
  methods: {
    handleDone() {
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.emit('success', this.suveryList);
      uni.navigateBack()
    },
    async getSurveyList() {
      let res = await getSurveyList()
      // res = res.slice(0, 10)
      let surveyList = []
      for (let index = 0; index < res.length; index++) {
        const { pitch, title, type, value } = res[index];
        const submitSurvey = { pitch, title, type, value };
        if (submitSurvey.type != 0) {
          submitSurvey.value = JSON.parse(submitSurvey.value);
          if (submitSurvey.type == 3) {
            submitSurvey.pitch = [];
          }
        }
        surveyList.push(submitSurvey);
      }
      this.suveryList = surveyList
    }
  }
}
</script>
<style lang="scss" scoped>
.suvery-list-wrap {
  background: #131e32;

  .form-box {
    background: #131e32;
    padding: 32rpx;
    padding-bottom: 180rpx;
  }

  .form-input-container {
    background-color: #fff;
    border-radius: 16rpx;
    padding: 0 32rpx;
    padding-bottom: 32rpx;
  }
}
</style>